package controller

import (
	"fmt"
	"ginblog/model"
	"ginblog/utils"
	"net/http"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

//IsLogin 中间件，判断是否登录
func IsLogin(c *gin.Context) {
	islogin := make(map[string]interface{})

	session := sessions.Default(c)
	name := fmt.Sprintf("%v", session.Get("sessionid"))

	ok, _ := model.GetUserLogin(string(name))
	islogin["IsLogin"] = ok

	c.Set("islogin", islogin)

}

//IndexHTML 首页
func IndexHTML(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", nil)
}

//BlogIndex blog首页
func BlogIndex(c *gin.Context) {
	// page := c.Query("page")
	// if page <= "0" {
	// 	page = "1"
	// }

	var ArtList []model.Article
	model.GetAllArtcle(&ArtList)

	islogin := c.GetStringMap("islogin")
	islogin["list"] = ArtList
	c.HTML(http.StatusOK, "home.html", islogin)
}

//GotoRegister 去注册页面
func GotoRegister(c *gin.Context) {
	c.HTML(http.StatusOK, "register.html", nil)
}

//CreateUser 用户注册
func CreateUser(c *gin.Context) {
	var user model.User
	c.Bind(&user)
	user.Createtime = time.Now().Unix()
	//用户密码加密
	user.Password = utils.MD5(user.Password)
	ok, _ := model.GetUserLogin(user.Username)
	if ok {
		// c.JSON(http.StatusOK, gin.H{"coed": 0, "message": "用户名已存在"})
		c.Redirect(http.StatusMovedPermanently, "/blog/register")
		return
	}
	if err := model.AddUser(&user); err != nil {
		// c.JSON(http.StatusOK, gin.H{"code": 0, "message": "注册失败"})
		c.Redirect(http.StatusMovedPermanently, "/blog/register")
	} else {
		// c.JSON(http.StatusOK, gin.H{"code": 1, "message": "注册成功"})
		c.Redirect(http.StatusMovedPermanently, "/blog/login")
	}
}

//GotoLogin 去用户登录页
func GotoLogin(c *gin.Context) {
	c.HTML(http.StatusOK, "login.html", nil)
}

//Login 登录并设置session
func Login(c *gin.Context) {
	session := sessions.Default(c)
	var user model.User
	c.Bind(&user)
	user.Password = utils.MD5(user.Password)
	err := model.GetUser(&user)
	if err != nil {
		c.Redirect(http.StatusMovedPermanently, "/blog/login")
	} else {
		session.Set("sessionid", user.Username)
		session.Save()
		c.Redirect(http.StatusMovedPermanently, "/blog")
	}
}

//UserExit 用户退出
func UserExit(c *gin.Context) {
	session := sessions.Default(c)
	session.Delete("sessionid")
	session.Save()
	//session.Clear()

	// fmt.Println("delete session...", session.Get("sessionid"))
	c.Redirect(http.StatusTemporaryRedirect, "/blog")

}

//GotoCreatArt 去添加文章页面
func GotoCreatArt(c *gin.Context) {
	islogin := c.GetStringMap("islogin")

	c.HTML(http.StatusOK, "write_article.html", islogin)
}

//CreateArticle 添加文章
func CreateArticle(c *gin.Context) {
	var art model.Article
	c.Bind(&art)
	art.Author = "admin"
	art.Createtime = time.Now().Unix()
	if err := model.AddArtcle(&art); err != nil {
		c.JSON(http.StatusOK, gin.H{"error": "添加错误"})
	} else {
		c.Redirect(http.StatusMovedPermanently, "/blog")
	}
}

//GotoAArtcle 去指定文章查看页面
func GotoAArtcle(c *gin.Context) {
	islogin := c.GetStringMap("islogin")

	var art model.Article
	id, _ := c.Params.Get("id")
	err := model.GetAArtcle(id, &art)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": "参数错误"})
	} else {
		islogin["art"] = art
		c.HTML(http.StatusOK, "show_article.html", islogin)
	}

}

//GotoUpArtcle 去指定文章修改页面
func GotoUpArtcle(c *gin.Context) {
	islogin := c.GetStringMap("islogin")

	id, _ := c.Params.Get("id")
	var art model.Article
	err := model.GetAArtcle(id, &art)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": "参数错误"})
	} else {
		islogin["art"] = art
		c.HTML(http.StatusOK, "write_article.html", islogin)
	}
}

//UpArticle 修改文章
func UpArticle(c *gin.Context) {
	var art model.Article
	c.Bind(&art)
	art.Author = "admin"
	art.Createtime = time.Now().Unix()
	if err := model.UpdateArtcle(&art); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "error"})
	} else {
		c.Redirect(http.StatusMovedPermanently, "/blog")
	}
}

//DelArtcle 删除文章
func DelArtcle(c *gin.Context) {
	id, _ := c.Params.Get("id")
	err := model.DeleteArtcle(id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": "删除错误"})
	} else {
		c.Redirect(http.StatusMovedPermanently, "/blog")
	}
}
